Quadrillion IO
·Follow
5 min read·Jun 1, 2023--
Bard is a large language model chatbot developed by Google AI. It is gaining more traction due to its speed and internet capabilities in comparison to Open AI’s ChatGPT. Google is yet to release an official API for developers to integrate Bard into their applications. However, there have been numerous projects that have built unofficial Python based APIs for Bard.
This article will guide you through building a replicate of Bard by using an unofficial Bard API and Streamlit. By following the steps in this article, you will be able to create a chatbot that can answer questions and generate text.
Note: This article assumes you are familiar with building Python applications in VS Code. If you need help with getting started, check out this guideline:
Get Started Tutorial for Python in Visual Studio CodeIn this tutorial, you will use Python 3 to create a simple Python "Hello World" application in Visual Studio Code. By…code.visualstudio.com
SetupCreate a new project with a virtual environment in VS Code. Then install the following libraries:
Streamlit is an open-source Python library that makes it easy to build web apps for machine learning and data science. It is a popular choice for data scientists and machine learning engineers because it is easy to use and can be deployed quickly.Streamlit_chat is a Python library that provides a simple way to add a chat feature to your Streamlit app. This can be useful for building chatbots or for providing a way for users to interact with your app in real time.The Bard API is a Python package that allows users to interact with Google’s Bard large language model. This unofficial API was developed by Daniel Park, and it is a valuable tool for developers who want to experiment with Bard in their applications.pip install streamlitpip install streamlit_chatpip install bardapiTo use the unofficial Bard API, we will first have to get a secure token that for the purposes of this article will be called the Bard API Key. Open Chrome and press F12 to trigger the development console. Next, visit https://bard.google.com/ and follow the steps below to get the Bard API Key:
Click on the Application TabIn the left panel, expand Cookies and click on Bard’s cookieIn the right panel, search for the “__Secure-1PSID”Copy the value of the cookieNow that we have the key, let’s store it in a configuration file. In your VS Code Project, create a new file called config.json then add the following code:
{"BARD_API_KEY": "INSERT_YOUR_BARD_API_KEY"}Note: You can skip this step and directly embed your API key into the code, but that goes against best practices.
API security best practices | Google Maps Platform | Google for DevelopersLearn how to secure and manage your Google Maps Platform API keys.developers.google.com
Bard ChatbotWe are ready to code our own chatbot! Create a new file called app.py and import these libraries.
import streamlit as stfrom streamlit_chat import messagefrom bardapi import Bardimport jsonFirst, we will have to read the Bard API Key from our configuration file and store it in a variable. Then we will initialize the Bard API by passing the key’s variable.
data = json.load(config_file)bard_api_key = data["BARD_API_KEY"]
bard = Bard(token=bard_api_key, timeout= 30)
Now that we have initialized the API, let’s create a function that takes a prompt as an input and calls the Bard API to get a response.
def generate_response(prompt):history = ""for user_response, bard_response in zip(st.session_state['user_responses'], st.session_state['bard_responses']):history += f"User: {user_response}\n"history += f"Bard Response: {bard_response}\n"response = bard.get_answer(f"Conversation History: {history}\n {prompt}")return response["content"]Notice how we include the history of our conversation into the prompt, so Bard has the full context of our discussion? Let’s create a function to clear the history so we can start a new discussion.
def on_btn_click():del st.session_state['user_responses']del st.session_state['bard_responses']Lastly, let’s create another function that handles user input and passes it to Bard for responses.
def on_input_change():user_input = st.session_state.user_inputoutput = generate_response(user_input)st.session_state.user_responses.append(user_input)st.session_state.bard_responses.append(output)st.session_state["user_input"] = ""We are almost done! The last step is to create the user interface for our web application using the code below:
# create the title for the pagest.title("Bard Chatbot")# create a button to clear the session state (chat history)st.button("New Topic", on_click=on_btn_click)if 'bard_responses' not in st.session_state:st.session_state['bard_responses'] = []if 'user_responses' not in st.session_state:st.session_state['user_responses'] = []
# create the text input fieldwith st.container():st.text_input("User Response:", on_change=on_input_change, key="user_input")
# show a loading spinner while the bard api is getting the response# then show the chat historywith st.spinner("Loading..."):if st.session_state['bard_responses']:for i in range(len(st.session_state['bard_responses'])-1, -1, -1):message(st.session_state["bard_responses"][i], key=str(i))message(st.session_state['user_responses'][i], is_user=True, key=str(i) + '_user')
Note: You can grab the full code on GitHub:
GitHub - quadrillion-io/Bard-ChatbotYou can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…github.com
Running Streamlit in VS CodeIn order to run our web application in VS Code, we first need to create the appropriate launch configuration:
Click on the run/debug icon in VS CodeClick on create a launch.json fileSelect “Module Debug a Python module by invoking it with ‘-m’.” from the prompt4. Enter “streamlit” for the module name
5. Add the following line of code to the launch.json file: , “args”: [“run”, “${file}”],
Note: Your launch.json file should looke like this.
{"version": "0.2.0","configurations": [{"name": "Python: Module","type": "python","request": "launch","module": "streamlit","args": ["run", "${file}"],"justMyCode": true}]}6. Save the file
Running Our Chatbot
Now, we are ready to run our code! Let’s give it a try.
Let’s also confirm that Bard is able to recall our conversation history.
Great! Now, it’s your turn to experiment with your very own Bard Chatbot.
Here are some questions to try:
What is the best way to solve world hunger?What is the future of artificial intelligence?Can you write me a poem about a cat?Can you compose a song about a dog?What is the meaning of life?